package site.marqstree.kotlin.library.base.ui.widget.newsflipper

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import android.widget.ViewFlipper
import org.jetbrains.anko.find
import site.marqstree.kotlin.library.base.ui.R
import site.marqstree.kotlin.library.base.util.GlideUtil


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.base.ui.widget.newsflipper
 * 文件名: NewsFlipperView
 * 创建者: marqstree
 * 创建时间: 2020/2/25 19:54
 * 描述:
    公告组件封装
 */
class NewsFlipper @JvmOverloads constructor(context: Context,
                                            attrs: AttributeSet? = null,
                                            defStyleAttr: Int = 0)
    : FrameLayout(context, attrs, defStyleAttr) {

    private val mFlipperView: ViewFlipper

    init {
        val rootView = View.inflate(context, R.layout.layout_news_flipper, null)
        mFlipperView = rootView.find(R.id.mFlipperView)
        mFlipperView.setInAnimation(context, R.anim.news_bottom_in)
        mFlipperView.setOutAnimation(context, R.anim.news_bottom_out)

        addView(rootView)
    }

    /*
    设置公告数据
    */
    fun setData(datas: Array<News>) {
        for (news in datas) {
            mFlipperView.addView(buildNewsView(news))
        }
        mFlipperView.startFlipping()
    }

    /*
    构建公告
    */
    private fun buildNewsView(news: News): View {
        val newsView: View = View.inflate(context, R.layout.item_flipper,null)
        val newsImg: ImageView = newsView.find(R.id.newsImg)
        val newsTv: TextView = newsView.find(R.id.newsTv)
        GlideUtil.loadImage(context,news.imgUrl,newsImg)
        newsTv.setText(news.text)
        return newsView
    }
}